package com.glela.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符工具类
 * 
 * @author liujie
 */
public class StringUtil
{
	public static String API_SZ_FCSEARCHPRODUCT = "";

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str)
	{
		if (str != null) {
			str = str.trim();
		}
		return StringUtils.isEmpty(str);
	}

	/**
	 * 判断字符串是否非空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotEmpty(String str)
	{
		return !StringUtil.isEmpty(str);
	}

	/**
	 * 根据分隔符获取数据
	 * 
	 * @param srcString
	 *            需要分割的字符串
	 * @param c
	 *            分割的字符比如：|，&等
	 * @return
	 */
	public static List<String> spiltString(String srcString, String c)
	{
		List<String> destString = new ArrayList<String>();
		if (srcString == null || c == null) {
			return null;
		}
		else {

			// 特殊字符需转义
			if ("\\".equals(c)) {
				c = "\\\\";
			}
			else {
				String rex = "[*+?|.$^]";
				Pattern pattern = Pattern.compile(rex);
				Matcher mat = pattern.matcher(c);
				if (mat.matches()) {
					// 特殊字符需转义
					c = "\\" + c;
				}
			}
			String[] temp = srcString.split(c);
			for (String str : temp) {
				if (!str.equals("")) {
					destString.add(str);
				}
			}
			return destString;
		}
	}
	
	/**
	 * 判断末尾是否是以  ，结束的
	 * @param string
	 * @return
	 */
	public static String subString(String string){
		if(string.endsWith(",")){
			return string.substring(0, string.length()-1);
		}else{
			return string;
		}
	}
	/**
	 * 替换为*字符{例如 18753993252 -> 187****52}
	 * @author gwq
	 * @param keepfirst 保留前几位
	 * @param replaceChar 其余替换为字符
	 * @param keepend 保留后几位
	 * @param str 目标字符
	 * @return
	 */
	public static String replaceChars(int keepfirst,int keepend,String str){
		if(StringUtils.isNotBlank(str)&&str.length()>(keepfirst+keepend)&&keepfirst>0&&keepend>0){
			return str.replaceAll("(\\d{"+keepfirst+"})\\d{"+(str.length()-keepfirst-keepend)+"}(\\d{"+keepend+"})", "$1****$2");		
		}
		return null;
	}

	/**
	 * 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0
	 * 
	 * @author gwq
	 * @param version1
	 * @param version2
	 * @return
	 */
	public static int compareVersion(String version1, String version2) throws Exception {
		if (version1 == null || version2 == null) {
			throw new Exception("compareVersion error:illegal params.");
		}
		String[] versionArray1 = version1.split("\\.");// 注意此处为正则匹配，不能用"."；
		String[] versionArray2 = version2.split("\\.");
		int idx = 0;
		int minLength = Math.min(versionArray1.length, versionArray2.length);// 取最小长度值
		int diff = 0;
		while (idx < minLength && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == 0// 先比较长度
				&& (diff = versionArray1[idx].compareTo(versionArray2[idx])) == 0) {// 再比较字符
			++idx;
		}
		// 如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；
		diff = (diff != 0) ? diff : versionArray1.length - versionArray2.length;
		return diff;
	}
}
